package extreme.tddscaffold;

public class SmallShop {

    /**
     * 萨弗拉斯
     */
    public static final String SULFURAS = "Sulfuras";

    /**
     * 后台门票
     */
    public static final String BACKSTAGEPASS = "Backstage pass";

    /**
     * 普通货物
     */
    public static final String GOODS = "Goods";

    /**
     * 最高价格值
     */
    public static final int MAXIMUM_PRICE = 50;

    /**
     * 最低价格值
     */
    public static final int MINIMUM_PRICE = 0;

    /**
     * 有效期——10天
     */
    public static final int SELL_IN_10 = 10;

    /**
     * 有效期——5天
     */
    public static final int SELL_IN_5 = 5;

    /**
     * 二倍
     */
    public static final int MULTIPLE_2 = 2;

    /**
     * 三倍
     */
    public static final int MULTIPLE_3 = 3;

    public Product getQualityAndSellIn(Product product) {
        // 最终价值：正常情况最终价值为价值+经过天数
        int finalQuality = product.getQuality() + product.getPassDay();
        // 保质期：正常情况保质期-经过天数
        int finalSellIn = product.getSellIn() - product.getPassDay();
        if (finalQuality >= MAXIMUM_PRICE) {
            finalQuality = MAXIMUM_PRICE;
        }
        switch (product.getName()) {
            case SULFURAS:
                // 萨弗拉斯最终价值
                finalQuality = product.getQuality();
                break;
            case BACKSTAGEPASS:
                // 计算后台门票的最终价值
                finalQuality = getBackstagePassFinalQuality(product);
                break;
            case GOODS:
                //计算普通商品的最终价值
                finalQuality = getGoodsFinalQuality(product, finalSellIn);
                break;
        }
        product.setQuality(finalQuality <= MINIMUM_PRICE ? MINIMUM_PRICE : finalQuality);
        product.setSellIn(finalSellIn);
        return product;
    }

    /**
     * 计算普通商品的最终价值
     *
     * @param product
     * @param finalSellIn
     * @return
     */
    private int getGoodsFinalQuality(Product product, int finalSellIn) {
        int finalQuality = 0;
        if (finalSellIn <= MINIMUM_PRICE) {
            finalQuality = product.getQuality() - product.getPassDay() * MULTIPLE_2;
        } else {
            finalQuality = product.getQuality() - product.getPassDay();

        }
        return finalQuality;
    }

    /**
     * 计算后台门票的最终价值
     *
     * @param product
     * @return
     */
    private int getBackstagePassFinalQuality(Product product) {
        int finalQuality = 0;
        if (product.getSellIn() - product.getPassDay() <= MINIMUM_PRICE) {
            finalQuality = MINIMUM_PRICE;

        } else {
            if (SELL_IN_10 >= product.getSellIn()) {
                finalQuality = product.getQuality() + (product.getPassDay() * MULTIPLE_2);

            }
            if (SELL_IN_5 >= product.getSellIn()) {
                finalQuality = product.getQuality() + (product.getPassDay() * MULTIPLE_3);
            }
        }
        return finalQuality;
    }
}
